<template>
  <div class="teacher-detail-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <div class="nav-left">
          <button class="back-btn" @click="goBack">
            <span class="back-icon">←</span>
            返回
          </button>
          <h1 class="page-title">教师详情</h1>
        </div>
        <div class="nav-right">
          <button class="user-btn" @click="handleProfileClick">
            <span class="user-icon">👤</span>我的
          </button>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="container">
        <!-- 教师基本信息卡片 -->
        <div class="teacher-info-card">
          <!-- 头像区域 -->
          <div class="teacher-avatar-section">
            <div class="avatar-placeholder">
              <span class="avatar-text">{{ avatarInitial }}</span>
            </div>
            <h2 class="teacher-name" :class="{ 'loading-text': loading }">
              {{ loading ? '加载中...' : (teacherInfo?.realName || '教师') }}
            </h2>
          </div>

          <!-- 基本信息 -->
          <div class="basic-info-section">
            <div class="info-row">
              <span class="info-label">工号：</span>
              <span class="info-value" :class="{ 'loading-text': loading }">
                {{ loading ? '加载中...' : (teacherInfo?.employeeId || '暂无') }}
              </span>
            </div>
            <div class="info-row">
              <span class="info-label">联系电话：</span>
              <span class="info-value" :class="{ 'loading-text': loading }">
                {{ loading ? '加载中...' : (teacherInfo?.phone || '暂无') }}
              </span>
            </div>
            <div class="info-row">
              <span class="info-label">邮箱：</span>
              <span class="info-value" :class="{ 'loading-text': loading }">
                {{ loading ? '加载中...' : (teacherInfo?.email || '暂无') }}
              </span>
            </div>
            <div class="info-row">
              <span class="info-label">专业职称：</span>
              <span class="info-value" :class="{ 'loading-text': loading }">
                {{ loading ? '加载中...' : (teacherInfo?.professionalTitle || '暂无') }}
              </span>
            </div>
          </div>

          <!-- 个人主页按钮 -->
          <div class="action-section" v-if="teacherInfo?.personalHomepage">
            <button class="homepage-btn" @click="goToPersonalHomepage">
              <span class="btn-icon">🌐</span>
              访问个人主页
            </button>
          </div>
        </div>

        <!-- 获奖情况 -->
        <div class="experience-section" v-if="awards.length > 0">
          <div class="section-header">
            <h3 class="section-title">🏆 获奖情况</h3>
            <span class="count-badge">{{ totalAwards }}项</span>
          </div>
          <div v-if="loading" class="loading-container">
            <div class="loading-spinner"></div>
            <div class="loading-text">加载中...</div>
          </div>
          <div v-else class="experience-list">
            <div 
              v-for="award in awards" 
              :key="award.id"
              class="experience-item award-item"
            >
              <div class="experience-title">{{ award.awardName }}</div>
              <div class="experience-subtitle">{{ award.awardLevel }} · {{ award.awardTime }}</div>
              <div class="experience-organization" v-if="award.awardingOrganization">
                颁奖机构：{{ award.awardingOrganization }}
              </div>
              <div class="experience-description" v-if="award.awardDescription">
                {{ award.awardDescription }}
              </div>
            </div>
          </div>
        </div>

        <!-- 参赛经历 -->
        <div class="experience-section" v-if="competitions.length > 0">
          <div class="section-header">
            <h3 class="section-title">🎯 参赛经历</h3>
            <span class="count-badge">{{ totalCompetitions }}项</span>
          </div>
          <div v-if="loading" class="loading-container">
            <div class="loading-spinner"></div>
            <div class="loading-text">加载中...</div>
          </div>
          <div v-else class="experience-list">
            <div 
              v-for="competition in competitions" 
              :key="competition.id"
              class="experience-item competition-item"
            >
              <div class="experience-title">{{ competition.competitionName }}</div>
              <div class="experience-subtitle">{{ competition.competitionLevel }} · {{ competition.participationRole }}</div>
              <div class="experience-time">时间：{{ competition.startTime }} 至 {{ competition.endTime }}</div>
              <div class="experience-result" v-if="competition.result">比赛结果：{{ competition.result }}</div>
              <div class="experience-description" v-if="competition.description">{{ competition.description }}</div>
            </div>
          </div>
        </div>

        <!-- 项目经历 -->
        <div class="experience-section" v-if="projects.length > 0">
          <div class="section-header">
            <h3 class="section-title">💼 项目经历</h3>
            <span class="count-badge">{{ totalProjects }}项</span>
          </div>
          <div v-if="loading" class="loading-container">
            <div class="loading-spinner"></div>
            <div class="loading-text">加载中...</div>
          </div>
          <div v-else class="experience-list">
            <div 
              v-for="project in projects" 
              :key="project.id"
              class="experience-item project-item"
            >
              <div class="experience-title">{{ project.projectName }}</div>
              <div class="experience-subtitle">{{ project.projectRole }}</div>
              <div class="experience-time">时间：{{ project.startTime }} 至 {{ project.endTime }}</div>
              <div class="experience-technology" v-if="project.technologiesUsed">
                技术栈：{{ project.technologiesUsed }}
              </div>
              <div class="experience-url" v-if="project.projectUrl">
                项目链接：<a :href="project.projectUrl" target="_blank" class="project-link">{{ project.projectUrl }}</a>
              </div>
              <div class="experience-description" v-if="project.projectDescription">
                {{ project.projectDescription }}
              </div>
            </div>
          </div>
        </div>

        <!-- 空状态提示 -->
        <div v-if="!loading && awards.length === 0 && competitions.length === 0 && projects.length === 0" class="empty-experience">
          <div class="empty-icon">📋</div>
          <div class="empty-text">该教师暂无相关经历信息</div>
        </div>
      </div>
    </main>

    <!-- 消息提示弹窗 -->
    <div v-if="showMessageModal" class="modal-overlay" @click="closeMessageModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header error-header">
          <h3>加载失败</h3>
          <button class="close-btn" @click="closeMessageModal">×</button>
        </div>
        <div class="modal-body">
          <div class="icon-wrapper">
            <div class="message-icon error-icon">✗</div>
          </div>
          <p class="modal-text">{{ errorMessage }}</p>
        </div>
        <div class="modal-footer">
          <button class="confirm-btn" @click="closeMessageModal">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { getTeacherCompleteInfoService } from '@/api/teacher.js'

const router = useRouter()
const route = useRoute()
const tokenStore = useTokenStore()

// 数据加载状态
const loading = ref(false)

// 消息提示弹窗控制
const showMessageModal = ref(false)
const errorMessage = ref('')

// 教师信息数据
const teacherInfo = ref(null)
const awards = ref([])
const competitions = ref([])
const projects = ref([])
const totalAwards = ref(0)
const totalCompetitions = ref(0)
const totalProjects = ref(0)

// 头像首字母
const avatarInitial = computed(() => {
  if (loading.value) return '...'
  const name = teacherInfo.value?.realName || '教师'
  return name.charAt(0)
})

// 页面挂载时获取教师信息
onMounted(async () => {
  // 检查登录状态
  if (!tokenStore.token) {
    router.push('/login')
    return
  }

  const employeeId = route.params.employeeId
  if (employeeId) {
    await fetchTeacherCompleteInfo(employeeId)
  } else {
    showErrorMessage('教师工号参数缺失')
  }
})

// 获取教师完整信息
const fetchTeacherCompleteInfo = async (employeeId) => {
  loading.value = true
  try {
    console.log('获取教师完整信息，工号:', employeeId)
    const result = await getTeacherCompleteInfoService(employeeId)
    console.log('教师完整信息API响应:', result)
    
    if (result.code === 200) {
      const data = result.data
      
      teacherInfo.value = data.basicInfo
      awards.value = data.awards || []
      competitions.value = data.competitions || []
      projects.value = data.projects || []
      totalAwards.value = data.totalAwards || 0
      totalCompetitions.value = data.totalCompetitions || 0
      totalProjects.value = data.totalProjects || 0
      
      console.log('教师信息加载成功:', teacherInfo.value?.realName)
    } else {
      console.error('获取教师完整信息失败:', result.msg)
      showErrorMessage(result.msg || '获取教师信息失败')
    }
  } catch (error) {
    console.error('获取教师完整信息异常:', error)
    showErrorMessage('获取教师信息时发生错误')
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 处理个人中心点击
const handleProfileClick = () => {
  router.push('/profile')
}

// 显示错误消息
const showErrorMessage = (text) => {
  errorMessage.value = text
  showMessageModal.value = true
}

// 关闭消息弹窗
const closeMessageModal = () => {
  showMessageModal.value = false
}

// 个人主页跳转
const goToPersonalHomepage = () => {
  const homepage = teacherInfo.value?.personalHomepage
  if (homepage) {
    // 如果地址不包含协议，自动添加https://
    const url = homepage.startsWith('http') ? homepage : `https://${homepage}`
    window.open(url, '_blank')
  } else {
    showErrorMessage('该教师未设置个人主页地址')
  }
}
</script>

<style scoped>
.teacher-detail-container {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 顶部导航栏 */
.header {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  padding: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.back-icon {
  font-size: 16px;
}

.page-title {
  color: white;
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.user-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.user-icon {
  font-size: 16px;
  opacity: 0.9;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
}

.container {
  width: 100%;
}

/* 教师信息卡片 */
.teacher-info-card {
  background: white;
  border-radius: 15px;
  padding: 30px;
  margin-bottom: 25px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.teacher-avatar-section {
  text-align: center;
  margin-bottom: 25px;
}

.avatar-placeholder {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 15px;
  box-shadow: 0 4px 12px rgba(98, 120, 219, 0.3);
}

.avatar-text {
  color: white;
  font-size: 40px;
  font-weight: bold;
}

.teacher-name {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
}

.basic-info-section {
  margin-bottom: 25px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  margin-bottom: 10px;
  background: #f8f9fa;
  border-radius: 10px;
  border-left: 4px solid #6278db;
}

.info-label {
  font-weight: 600;
  color: #333;
  font-size: 16px;
}

.info-value {
  color: #666;
  font-size: 16px;
  text-align: right;
  word-break: break-all;
}

.loading-text {
  color: #999;
  font-style: italic;
}

.action-section {
  text-align: center;
}

.homepage-btn {
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.homepage-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(40, 167, 69, 0.3);
}

.btn-icon {
  font-size: 18px;
}

/* 经历区域 */
.experience-section {
  background: white;
  border-radius: 15px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f0f0;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-badge {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  color: white;
  padding: 6px 15px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  gap: 15px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #6278db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.experience-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.experience-item {
  padding: 20px;
  border-radius: 12px;
  border: 1px solid #e9ecef;
  transition: all 0.3s ease;
}

.experience-item:hover {
  border-color: #6278db;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(98, 120, 219, 0.15);
}

.award-item {
  background: linear-gradient(135deg, rgba(255, 193, 7, 0.1) 0%, rgba(255, 193, 7, 0.05) 100%);
  border-left: 4px solid #ffc107;
}

.competition-item {
  background: linear-gradient(135deg, rgba(40, 167, 69, 0.1) 0%, rgba(40, 167, 69, 0.05) 100%);
  border-left: 4px solid #28a745;
}

.project-item {
  background: linear-gradient(135deg, rgba(98, 120, 219, 0.1) 0%, rgba(98, 120, 219, 0.05) 100%);
  border-left: 4px solid #6278db;
}

.experience-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.experience-subtitle {
  font-size: 16px;
  color: #666;
  margin-bottom: 8px;
  font-weight: 500;
}

.experience-time,
.experience-organization,
.experience-result,
.experience-technology,
.experience-url {
  font-size: 14px;
  color: #777;
  margin-bottom: 6px;
  line-height: 1.4;
}

.experience-result {
  color: #28a745;
  font-weight: 500;
}

.experience-description {
  font-size: 15px;
  color: #555;
  line-height: 1.5;
  margin-top: 10px;
}

.project-link {
  color: #6278db;
  text-decoration: none;
  word-break: break-all;
  transition: color 0.2s ease;
}

.project-link:hover {
  color: #5a6fd8;
  text-decoration: underline;
}

/* 空状态 */
.empty-experience {
  text-align: center;
  padding: 60px 20px;
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
  opacity: 0.6;
}

.empty-text {
  font-size: 18px;
  color: #666;
  font-weight: 500;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
}

.modal-content {
  background: white;
  border-radius: 20px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.3s ease-out;
  position: relative;
  overflow: hidden;
}

.modal-header {
  background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
  color: white;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s ease;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  padding: 30px 20px;
  text-align: center;
}

.icon-wrapper {
  margin-bottom: 20px;
}

.message-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 30px;
  font-weight: bold;
  margin: 0 auto;
  background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
}

.modal-text {
  font-size: 18px;
  color: #333;
  margin: 0 0 10px 0;
  font-weight: 600;
}

.modal-footer {
  padding: 0 20px 20px;
  display: flex;
  gap: 12px;
  justify-content: center;
}

.confirm-btn {
  flex: 1;
  max-width: 120px;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #6278db;
  color: white;
  border: none;
}

.confirm-btn:hover {
  background: #5a6fd8;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(98, 120, 219, 0.3);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: scale(0.7);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-container {
    padding: 10px 15px;
  }
  
  .nav-left {
    gap: 15px;
  }
  
  .page-title {
    font-size: 18px;
  }
  
  .main-content {
    padding: 20px 15px;
  }
  
  .teacher-info-card {
    padding: 25px 20px;
  }
  
  .avatar-placeholder {
    width: 80px;
    height: 80px;
  }
  
  .avatar-text {
    font-size: 32px;
  }
  
  .teacher-name {
    font-size: 24px;
  }
  
  .info-row {
    padding: 12px 15px;
  }
  
  .info-label,
  .info-value {
    font-size: 14px;
  }
  
  .experience-section {
    padding: 20px 15px;
  }
  
  .section-title {
    font-size: 18px;
  }
  
  .experience-item {
    padding: 15px;
  }
  
  .experience-title {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .nav-left {
    gap: 10px;
  }
  
  .back-btn {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .page-title {
    font-size: 16px;
  }
  
  .main-content {
    padding: 15px 10px;
  }
  
  .teacher-info-card {
    padding: 20px 15px;
  }
  
  .avatar-placeholder {
    width: 70px;
    height: 70px;
  }
  
  .avatar-text {
    font-size: 28px;
  }
  
  .teacher-name {
    font-size: 20px;
  }
  
  .info-row {
    padding: 10px 12px;
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  
  .info-value {
    text-align: left;
  }
  
  .homepage-btn {
    padding: 10px 20px;
    font-size: 14px;
  }
  
  .experience-section {
    padding: 15px 12px;
  }
  
  .section-title {
    font-size: 16px;
  }
  
  .experience-item {
    padding: 12px;
  }
  
  .experience-title {
    font-size: 15px;
  }
  
  .modal-content {
    width: 95%;
    margin: 20px;
  }
  
  .modal-header {
    padding: 15px;
  }
  
  .modal-header h3 {
    font-size: 16px;
  }
  
  .modal-body {
    padding: 25px 15px;
  }
  
  .modal-text {
    font-size: 16px;
  }
  
  .confirm-btn {
    padding: 10px 15px;
    font-size: 14px;
  }
}
</style>